package org.cttic.visualization.visualization.util;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;
import org.bson.Document;
import org.cttic.visualization.visualization.common.T_mysqlData;
import org.cttic.visualization.visualization.common.realTimeData;
import org.cttic.visualization.visualization.common.sectionBillQuery;
import org.cttic.visualization.visualization.common.trackQueryData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-05-06 17:29
 */
@Component
public class z_DataUtil {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private queryBillMileUtil queryBillMileUtil;
    @Autowired
    private VehicleInformationUtil vehicleInformationUtil;
    @Autowired
    private  chargingUtil chargingUtil;
    public realTimeData sendDataUtil(String value) {
        realTimeData realTimeData = new realTimeData();
        JSONObject data = ParseJsonDataUtil.getJsonData(value);
        String vehicleId = data.getString("vehiclePlate");
        int vehicleIdColor = data.getIntValue("vehicleColor");
//        int vehicleIdColor =1;

        String transTime = data.getString("transTime");
        int pointType = data.getIntValue("pointType");
        String gantryId = data.getString("gantryId");
        String gantryName = data.getString("gantryName");
        String latitude = data.getString("latitude");
        String longitude = data.getString("longitude");
        double lat = GpsCoordinateUtils.calWGS84toGCJ02Lat(Double.parseDouble(latitude), Double.parseDouble(longitude));
        double log = GpsCoordinateUtils.calWGS84toGCJ02Log(Double.parseDouble(latitude), Double.parseDouble(longitude));
//        String endLat = data.getString("endLat");
        int enExFlag = data.getIntValue("enExFlag");
        String provinceId = null;
        String roadSectionId = null;
        String pileNumber = null;
        String direction = null;
        String billAmount =null;
        String transFee=null;
        //todo ****************查询计费信息****************
        sectionBillQuery sectionBillQuery = new sectionBillQuery();
        sectionBillQuery.setVehPlate(vehicleId);
        sectionBillQuery.setPlateColor(ConvertColor.getColor(vehicleIdColor));
        sectionBillQuery.setCurrentsTime(DateUtil.timeToT(transTime));
        //查询mysql 门架基础信息表 T_GANTRY_MOUNTING

        if (pointType == 1||pointType==2) {

            String sql = "SELECT gantryId,provinceId,tollIntervals,roadSectionId,pileNumber FROM `T_GANTRY_MOUNTING` " +
                    "where  gantryId= '" +
                    gantryId + "'";
            List<T_mysqlData> mysqlData = jdbcTemplate.query(sql, new BeanPropertyRowMapper(T_mysqlData.class));
            if(mysqlData!=null&&mysqlData.size()!=0){

                T_mysqlData t_mysqlData = mysqlData.get(0);
                provinceId = t_mysqlData.getProvinceId();
                roadSectionId = t_mysqlData.getRoadSectionId();
                pileNumber = t_mysqlData.getPileNumber();
                direction = gantryId.substring(14, 15);
                //G000444001001010040
                sectionBillQuery.setSectionDirection(gantryId.substring(14, 15));

                sectionBillQuery.setAdminCode(t_mysqlData.getProvinceId());        //省域编码
                sectionBillQuery.setSectionCode(t_mysqlData.getRoadSectionId());       //路段编号
                String tollIntervals = t_mysqlData.getTollIntervals();
                sectionBillQuery.setTollIntervals(tollIntervals);      //路段单元编码
                queryBillMileUtil.queryBillMile(tollIntervals,sectionBillQuery);    //添加里程数
            }
            //添加车辆信息
            vehicleInformationUtil.informationQuery(vehicleId,vehicleIdColor+"",sectionBillQuery);
            //调用计费
            String urlString ="http://192.168.195.30:9042/hmop/mmp/api/tb/getJiangXiNationalBilling?vehPlate="+vehicleId+"&plateColor="+vehicleIdColor+
                    "&adminCode="+sectionBillQuery.getAdminCode()+"&sectionCode="+sectionBillQuery.getSectionCode()+
                    "&sectionDirection="+sectionBillQuery.getSectionDirection()+"&billMile="+sectionBillQuery.getBillMile()+
                    "&currentsTime="+sectionBillQuery.getCurrentsTime()+"&vehCode="+sectionBillQuery.getVehCode()+
                    "&vehicleType="+sectionBillQuery.getVehicleType()+"&actualAxleNum="+sectionBillQuery.getActualAxleNum()+
                    "&mediumType="+sectionBillQuery.getMediumType()+"&tollIntervals="+sectionBillQuery.getTollIntervals();
            if(sectionBillQuery.getVehCode()!=null&&sectionBillQuery.getVehicleType()!=null&&sectionBillQuery.getMediumType()!=null){
                if(sectionBillQuery.getActualAxleNum()!=null){
                    transFee = chargingUtil.queryTransFee(urlString);
                }else {
                    sectionBillQuery.setActualAxleNum(3);
                    String urlString2 ="http://192.168.195.30:9042/hmop/mmp/api/tb/getJiangXiNationalBilling?vehPlate="+vehicleId+"&plateColor="+vehicleIdColor+
                            "&adminCode="+sectionBillQuery.getAdminCode()+"&sectionCode="+sectionBillQuery.getSectionCode()+
                            "&sectionDirection="+sectionBillQuery.getSectionDirection()+"&billMile="+sectionBillQuery.getBillMile()+
                            "&currentsTime="+sectionBillQuery.getCurrentsTime()+"&vehCode="+sectionBillQuery.getVehCode()+
                            "&vehicleType="+sectionBillQuery.getVehicleType()+"&actualAxleNum="+sectionBillQuery.getActualAxleNum()+
                            "&mediumType="+sectionBillQuery.getMediumType()+"&tollIntervals="+sectionBillQuery.getTollIntervals();
                    transFee = chargingUtil.queryTransFee(urlString2);
                }

            }

        } else if (pointType == 3) {
            String sql = "SELECT stationId,provinceId,stakeNum FROM `T_STATION` " +
                    "where  stationId= '" +
                    gantryId + "'";
            //List<Object> mysqlData = jdbcTemplate.query(sql, new BeanPropertyRowMapper(Object.class));
            // Object t_mysqlData = mysqlData.get(0);
            List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
            if(maps!=null&&maps.size()!=0){
                Map<String, Object> stringObjectMap = maps.get(0);
//            sectionId = stringObjectMap.get("stationId").toString();
                provinceId = stringObjectMap.get("provinceId").toString();
                pileNumber = stringObjectMap.get("stakeNum") == null ? "": stringObjectMap.get("stakeNum").toString();
//            roadSectionId = t_mysqlData.getRoadSectionId();
                if (enExFlag == 1) {
                    direction = "1";
                } else if (enExFlag == 2) {
                    direction = "2";
                }
            }
        }

        realTimeData.setCmd("122");

        realTimeData.setProvinceId(provinceId);     //todo 省编码
//        realTimeData.setSessionId();
        realTimeData.setVehId(vehicleId + "_" + ConvertColor.getColor(vehicleIdColor));
        realTimeData.setVehStatus("0");
        realTimeData.setTransTime(DateUtil.timeToT(transTime));

        realTimeData.setPointType(pointType+"");
        realTimeData.setGantryId(gantryId);
        realTimeData.setGantryName(gantryName);

        if (enExFlag ==1||enExFlag==2 ) {
            realTimeData.setEnExFlag(enExFlag + "");
        }
        realTimeData.setSectionCode(roadSectionId);        //todo 路段编号
        realTimeData.setPileNum(pileNumber);            //todo 门架桩号
        realTimeData.setDirection(direction);          //todo 门架方向
        realTimeData.setLat(lat+"");
        realTimeData.setLng(log+"");
        realTimeData.setTransFee(transFee);         //todo 屏显金额
//        double x = Math.random() * 10;
//        if(x>5){
//            realTimeData.setTransFee("**.**");      //todo 屏显金额
//        }else {
//            realTimeData.setTransFee("***.**");      //todo 屏显金额
//        }
//        realTimeData.setTransFee(billAmount);
//        realTimeData.setEndLat(endLat);
        return realTimeData;


    }
    public static List<trackQueryData.vehLocation> z_trackQueryDataUtil(FindIterable<Document> documents) {
        List<trackQueryData.vehLocation> list = new ArrayList<>();
        MongoCursor<Document> iterator = documents.iterator();
        while (iterator.hasNext()) {
            trackQueryData.vehLocation vehLocation = new trackQueryData.vehLocation();
            String json = iterator.next().toJson();
            JSONObject data = ParseJsonDataUtil.getJsonData(json);
            String gpsTime = data.getString("gpsTime");
            String[] split = gpsTime.split(":");
            String s = split[split.length - 1];
            String[] split1 = s.split("}");
            String date = DateUtil.StampToDate(Long.parseLong(split1[0]));

//            String transTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(gpsTime);
            String lat = data.getDouble("latitude")+"";

            String lng = data.getDouble("longitude") + "";

            String vec = data.getIntValue("speed")+"";
            String dir = data.getIntValue("direction")+"";
            String dis = data.getString("totalDistance");
            vehLocation.setTransTime(DateUtil.timeToT(date));
            vehLocation.setDir(dir);
            vehLocation.setDis(dis);
            vehLocation.setLat(lat);
            vehLocation.setLng(lng);
            vehLocation.setVec(vec);
            list.add(vehLocation);
        }
        return list;
    }

}
